package web.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by Administrator on 2016/10/10.
 */
public class PathUtil {
    private static PathUtil pu;

    private PathUtil() {
    }

    public static PathUtil getInstance() {
        if(pu == null) {
            pu = new PathUtil();
        }

        return pu;
    }

    public String getWebClassesPath() {
        String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        return path;
    }

    public String getWebInfPath() {
        String path = this.getWebClassesPath();
        //System.out.println("path:"+path);
        if(path.indexOf("WEB-INF") > 0) {
            path = path.substring(0, path.indexOf("WEB-INF") + 8);
        } else {
            //System.out.println("路径获取错误");
        }

        return path;
    }

    public String getWebRoot() {
        String path = this.getWebClassesPath();
        if(path.indexOf("WEB-INF") > 0) {
            path = path.substring(0, path.indexOf("WEB-INF/classes"));
        } else {
            System.out.println("路径获取错误");
        }

        return path;
    }

    public static String getPath() {
        try {
            return URLDecoder.decode(getInstance().getWebRoot(), "UTF-8");
        } catch (UnsupportedEncodingException var1) {
            var1.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(getInstance().getWebClassesPath());
        System.out.println(getInstance().getWebInfPath());
        System.out.println(getInstance().getWebRoot());
    }
}
